﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using LPV_POS.Baocao;
using System.IO;

namespace LPV_POS.mh_pos.mh_inbaocao
{
    class mh_Phieu_dat_ban
    {
        public void InPhieu(DataTable tbPhieu, DataTable tbPhieu_CT)
        {
            if (tbPhieu == null)
                return;
            if (tbPhieu.Rows.Count == 0)
                return;

            /////////////////
            //tbPhieu_CT.TableName = "Phieu_Dat_Ban_CT";
            //tbPhieu_CT.WriteXmlSchema(@"D:\Project\LPV-POS\LPV-POS\Baocao\Phieu_Dat_Ban_CT.xsd");
            ////////////////
            
            cpublic ocpublic = new cpublic();
            bc_Phieu_dat_ban rpt = new bc_Phieu_dat_ban();
            //
            DataTable dt = get_Logo();
            if (dt.Rows.Count > 0)
                rpt.Subreports[0].SetDataSource(dt);
            //
            rpt.SetDataSource(tbPhieu_CT);
            //
            rpt.DataDefinition.FormulaFields["KH_TEN"].Text = "'" + tbPhieu.Rows[0]["Ten_KH"] + "'";
            rpt.DataDefinition.FormulaFields["KH_SDT"].Text = "'" + tbPhieu.Rows[0]["SO_DIEN_THOAI"] + "'";
            DateTime tNGAY_GIO_DEN = Variable.ngay_gio_hienhanh;
            DateTime.TryParse(tbPhieu.Rows[0]["NGAY_GIO_DEN"]+"", out tNGAY_GIO_DEN);
            rpt.DataDefinition.FormulaFields["NGAY"].Text = "'" + tNGAY_GIO_DEN.ToString("dd/MM/yyyy HH:mm") + "'";
            rpt.DataDefinition.FormulaFields["BAN"].Text = "'" + tbPhieu.Rows[0]["OBJNAME"] + "'";
            float fDAT_COC = 0;
            float.TryParse(tbPhieu.Rows[0]["DAT_COC"] + "", out fDAT_COC);
            rpt.DataDefinition.FormulaFields["DATCOC"].Text = "'" + fDAT_COC.ToString("N0") + "'";
            //
            #region "   PrinterDefault"
            System.Drawing.Printing.PrinterSettings cls = new System.Drawing.Printing.PrinterSettings();
            string sPrinterDefault = cls.PrinterName;
            rpt.PrintOptions.PrinterName = sPrinterDefault;
            #endregion
            try
            {
                if (ocpublic.Registry_Get("chkCrystalReport_Display") + "" == "True")
                {
                    mh_View frm = new mh_View(rpt);
                    frm.Show();
                }
            }
            catch (Exception ex)
            {
            }
            rpt.PrintToPrinter(0, true, 1, 100);
        }

        #region "   get_Logo"
        private DataTable get_Logo()
        {
            DataTable dt = new DataTable();
            DataRow row;
            dt.Columns.Add("Hinh", System.Type.GetType("System.Byte[]"));
            dt.Columns.Add("HinhDetail", System.Type.GetType("System.Byte[]"));
            row = dt.NewRow();

            //Create Filestream to read image
            FileStream fs;

            //Create variable BinaryReader to read bytes of image
            BinaryReader br;

            //Check exists of image

            if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "logo1.bmp"))
            {
                //open image in file stream
                fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "logo1.bmp", FileMode.Open);
            }
            else //if image not exists show image Nopoto.img
                return dt;

            br = new BinaryReader(fs);
            byte[] b = new byte[fs.Length];
            b = br.ReadBytes(Convert.ToInt32(fs.Length));
            row["Hinh"] = b;
            row["HinhDetail"] = b;
            dt.Rows.Add(row);
            fs.Dispose();
            return dt;
        }
        #endregion


    }
}
